<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>后台管理员登录</title>
		<meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link rel="stylesheet" href="css/bootstrap.min.css" />
		<link rel="stylesheet" href="css/bootstrap-responsive.min.css" />
        <link rel="stylesheet" href="css/unicorn.login.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
    <body>
        <div id="logo" style="margin-top: 250px;">
            <img src="${pageContext.request.contextPath}/back/img/logo.png" alt="" />
        </div>
        <div id="loginbox" >            
            <form id="loginform" class="form-vertical" method="post" action="${pageContext.request.contextPath}/admin" >
             <!-- 隐藏域 -->
                <input type="hidden" name="op" value="adminLogin"/>
				<p>管理员登录</p>
                <div class="control-group">
                    <div class="controls">
                        <div class="input-prepend">
                            <span class="add-on"><i class="icon-user"></i></span><input name="username" id="username" type="text" placeholder="用户名" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <div class="input-prepend">
                            <span class="add-on"><i class="icon-lock"></i></span><input name="password" id="password" type="password" placeholder="密码 " />
                        </div>
                    </div>
                </div>
                <div class="form-actions">
					<!--如果被选中,默认的value属性值为on -->
                	<input type="checkbox" id="remember" name="remember" style="margin-top:-1px ;margin-left: -250px;"/>记住密码
                	<span class="pull-right"><input type="submit" class="btn btn-inverse" value="登录"/></span>
                </div>
            </form>
            
        </div>
        
        <script src="js/jquery.min.js"></script>  
        <script src="js/unicorn.login.js"></script> 
    </body>
    
    <script>
    	var loginErrorMessage = "${param.loginErrorMessage}";
    	if(loginErrorMessage!=""){
    		alert("用户名或密码错误，请重新登录！");
     		location.href = "${pageContext.request.contextPath}/back/login.jsp";
    	}
    	var noLoginMessage = "${param.noLoginMessage}";  // ${param.参数名} EL表达式获取页面URL上的参数值
    	if(noLoginMessage!=""){
    		alert("请先登录!");
    		location.href="${pageContext.request.contextPath}/back/login.jsp";
    	}
    	
    	window.onload = function(){
    		//获取客户端所有的cookie
    		var cookies = document.cookie;
    		//判断cookie是否有内容
    		if(cookies.length!=0){
    			//存放的cookie信息cookie1=value;cookie2=value;
    			var cs = cookies.trim().split(";");
    			//获取第一个
    			var c = cs[0].split("=");
    			//填充到页面
    			document.getElementById("username").value = c[0];
    			document.getElementById("password").value = c[1];
    			document.getElementById("remember").checked = true;
    		}
    	}
    	
    	//对用户名输入失去焦点事件
    	document.getElementById("username").onblur = function(){
    		// 获取输入的用户名
    		var username = this.value;
    		console.log(username);
    		// 获取当前username是否有对应的cookie
    		var value = getCookie(username);
    		if(value!=""){
    			// 表示有当前这个useranme的key
    			document.getElementById("password").value = value;
    			document.getElementById("remember").checked = true;
    		}else{
    			document.getElementById("password").value = "";
    			document.getElementById("remember").checked = false;
    		}
    		
    	}
    	
    	// 创建一个函数用于返回指定 cookie 的值：
    	function getCookie(cname) {
    	  var name = cname + "="; // username1=
    		
    	  // document.cookie返回"cookie1=value; cookie2=value; cookie3=value"
    	  var ca = document.cookie.split(';'); // 切割后得到数组,数组有两个元素[ "username1=king" "username2=queen" ]
    	  
    	  for(var i = 0; i < ca.length; i++) {
    		  
    	    var c = ca[i].trim(); // ca[0]是username1=king  ca[1]是username2=queen
    	    
    	    if (c.indexOf(name) == 0) {
    	    	return c.substring(name.length,c.length);
    	    }
    	  }
    	  return "";
    	}
    </script>
</html>
